<template>
  <div class="container">
    <el-card>
      <!-- 顶部搜索栏 -->
      <el-form
        :inline="true"
        :span="16"
        :model="page"
        class="page"
        ref="searchForm"
      >
        <el-form-item label="关键字">
          <el-input placeholder="请输入关键字" v-model="page.tagName" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select placeholder="请选择" v-model="page.state">
            <el-option label="启用" :value="0" />
            <el-option label="禁用" :value="1" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="clearBtn">清除</el-button>
          <el-button type="primary" @click="searchBtn">查询</el-button>
        </el-form-item>
        <!-- 新增技巧 -->
        <el-button
          class="addSkill"
          type="success"
          icon="el-icon-edit"
          @click="isAddShow = true"
          >新增标签</el-button
        >
      </el-form>
      <!-- 数据条数 -->
      <div class="dataCount">
        <i class="el-icon-info"></i> 数据一共有{{ count }}条
      </div>
      <!-- 数据列表 -->
      <el-table
        ref="singleTable"
        :data="tagsList"
        highlight-current-row
        style="width: 100%"
        type="flex"
      >
        <el-table-column type="index" :span="1" label="序号" align="center">
        </el-table-column>
        <el-table-column
          property="subjectName"
          label="所属学科"
          :span="2"
          align="center"
        >
        </el-table-column>
        <el-table-column
          property="tagName"
          label="标签名称"
          :span="2"
          align="center"
        >
        </el-table-column>
        <el-table-column
          property="username"
          label="创建者"
          :span="2"
          align="center"
        >
        </el-table-column>
        <el-table-column label="创建日期" :span="2" align="center">
          <!-- 作用域插槽 -->
          <template slot-scope="{ row }">
            <!-- 时间格式化 -->
            {{ row.addDate | parseTimeByString }}
          </template>
        </el-table-column>
        <el-table-column property="state" label="状态" :span="2" align="center">
          <template slot-scope="{ row }">
            已{{ row.state === 1 ? "禁用" : "启用" }}
          </template>
        </el-table-column>
        <el-table-column
          property="address"
          label="操作"
          :span="5"
          align="center"
        >
          <template slot-scope="{ row }">
            <el-button type="text" size="small" @click="changeState(row)"
              >禁用</el-button
            >
            <el-button
              type="text"
              size="small"
              @click="editTag(row)"
              :disabled="row.state !== 0"
              >修改</el-button
            >
            <el-button
              type="text"
              size="small"
              @click="delTags(row)"
              :disabled="row.state !== 0"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 页码 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="page.pagesize"
        layout="prev, pager, next, sizes, jumper"
        type="flex"
        right="10px"
        :total="count"
        class="block"
        hideOnSinglePage
      />
      <!-- 新增标签 -->
      <tags-add
        ref="tags"
        :is-add-show.sync="isAddShow"
        @addTags="getTagsList"
      />
    </el-card>
  </div>
</template>

<script>
import { getTagsList, remove, changeState } from '@/api/hmmm/tags'
import tagsAdd from '@/module-hmmm/components/tags-add.vue'
import state from '@/api/base/baseApi'
export default {
  components: {
    tagsAdd
  },
  data () {
    return {
      tagsList: [], // 标签列表
      currentPage: 1, // 当前跳转页码
      page: {
        // 获取当前列表数据参数
        page: 1, // 当前页码
        pagesize: 10, // 每页数量
        subjectID: null,
        tagName: '',
        state: null
      },
      count: 0, // 总数
      isAddShow: false, // 控制新增弹层显示
      state // 启用/禁用状态
    }
  },
  created () {
    this.getTagsList(this.page)
  },
  methods: {
    // 获取表格数据渲染页面
    async getTagsList () {
      const { data } = await getTagsList(this.page)
      this.tagsList = data.items
      this.count = data.counts
      if (!this.tagsList.length && this.page.page > 1) {
        this.page.page = this.page.page - 1
        this.getTagsList()
      }
    },
    // 提交搜索
    async searchBtn () {
      // if (this.page.tagName === '' && this.page.state === null) {
      //   return
      // }
      await this.getTagsList(this.page)
    },
    // 清空搜索
    clearBtn () {
      this.page = {
        page: this.page.page,
        pagesize: this.page.pagesize,
        subjectID: null,
        tagName: '',
        state: null
      }
    },
    // 切换每页显示条数
    handleSizeChange (val) {
      this.page.pagesize = val
      this.getTagsList(this.page)
    },
    // 切换页码
    handleCurrentChange (val) {
      this.page.page = val
      this.getTagsList(this.page)
    },
    // 删除
    async delTags (row) {
      try {
        await this.$confirm('删除后不可恢复，您确定删除该文章嘛？')
        await remove(row)
        this.getTagsList()
        this.$message.success('删除成功')
      } catch (err) {
        this.$message.error('下次再来')
      }
    },
    // 修改文章状态 (传参id和state)
    async changeState (row) {
      await changeState({
        id: row.id,
        state: row.state === 1 ? 0 : 1
      })
      row.state = row.state === 1 ? 0 : 1
    },
    // 修改标签
    editTag (row) {
      this.isAddShow = true
      // 传递节点
      this.$refs.tags.editTag(row)
    }
  }
}
</script>

<style lang='scss'>
.container {
  position: relative;
  font-size: 16px;
  .page {
    margin-left: 40px;
  }

  .addSkill {
    position: absolute;
    right: 25px;
  }
  .dataCount {
    width: 100%;
    height: 30px;
    line-height: 30px;
    padding-left: 10px;
    background-color: rgb(244, 244, 245);
    color: rgb(144, 147, 153) !important;
    i {
      color: rgb(144, 147, 153) !important;
    }
  }
  .block {
    float: right;
  }
}
</style>
